package com.arvato.wxwork.service.member;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.wxwork.bean.bean_do.member.WxworkTagDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkUserTagDo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkManualTagVo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkTagVo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkUserTagVo;
import com.arvato.wxwork.mapper.member.WxworkTagMapper;
import com.arvato.wxwork.mapper.member.WxworkUserTagMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class WxworkTagService {

	@Autowired
	private WxworkTagMapper wxworkTagMapper;

	@Autowired
	private WxworkUserTagMapper wxworkUserTagMapper;
	
	/**
	 * 根据条件批量查询（分页）
	 * @param query
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageData listTagByQuery(Query query, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<WxworkTagVo> list = wxworkTagMapper.listTagByQuery(query);
		PageInfo<WxworkTagVo> pageInfo = new PageInfo<WxworkTagVo>(list);
		PageData pageData = new PageData(pageInfo.getTotal(), pageInfo.getList());
		return pageData;
	}
	
	/**
	 * 根据条件批量查询（不分页）
	 * @param query
	 * @return
	 */
	public List<WxworkTagVo> listTagByQuery(Query query) {
		List<WxworkTagVo> list = wxworkTagMapper.listTagByQuery(query);
		return list;
	}
	
	/**
	 * 根据条件查询user-tag的绑定关心
	 * @param query
	 * @return
	 */
	public List<WxworkUserTagVo> listUserTagByQuery(Query query) {
		List<WxworkUserTagVo> list = wxworkUserTagMapper.listUserTagByQuery(query);
		return list;
	}
	
	/**
	 * 根据条件ID查询tag
	 * @param id
	 * @return
	 */
	public WxworkTagVo getTagById(String id) {
		WxworkTagVo tagVo = wxworkTagMapper.getTagById(id);
		return tagVo;
	}
	
	/**
	 * 更新tag
	 * @param dto
	 * @return
	 */
	public int update(WxworkTagDo tagDo) {
		return wxworkTagMapper.update(tagDo);
	}
	
	/**
	 * 新增一个tag
	 * @param dto
	 * @return
	 */
	public int insert(WxworkTagDo tagDo) {
		return wxworkTagMapper.insert(tagDo);
	}
	
	/**
	 * 根据ID删除tag
	 * @param id
	 * @return
	 */
	public int delete(String id) {
		return wxworkTagMapper.delete(id);
	}
	
	/**
	 * 根据查询条件删除tag
	 * @param query
	 * @return
	 */
	public int deleteByQuery(Query query) {
		return wxworkTagMapper.deleteByQuery(query);
	}

	/**
	 * 将一个tag绑定给一个user
	 * 先查询tag是否存在 存在直接取ID 不存在就插入
	 * @param tagDo
	 * @param userTagDo
	 * @return
	 */
	public int bindTag(WxworkTagDo tagDo, WxworkUserTagDo userTagDo) {
		// 首先查询当前绑定的tag是否存在
		Query query = new Query();
		query.put("tagName", tagDo.getTagName());
		List<WxworkTagVo> list = wxworkTagMapper.listTagByQuery(query);
		if(list.size() >= 1) {
			userTagDo.setTagId(list.get(0).getId());
		}else {
			int row = wxworkTagMapper.insert(tagDo);
			if(row == 1) {
				userTagDo.setTagId(tagDo.getId());
			}
		}
		// 标签新增成功的话 就执行绑定
		if(userTagDo.getTagId() != null) {
			return wxworkUserTagMapper.insert(userTagDo);
		}
		return 0;
	}
	
	/**
	 * 解除用户和标签的绑定
	 * @param bindId
	 * @return
	 */
	public int unbindTag(String bindId) {
		return wxworkUserTagMapper.delete(bindId);
	}
	
	public void importExcelData(Iterator<WxworkTagVo> it) {
		while (it.hasNext()) {
			WxworkTagVo str = it.next();
			Query query = new Query();
			query.put("id", str.getId());
			wxworkTagMapper.deleteByQuery(query);
			WxworkTagDo dto = new WxworkTagDo();
			dto.setTagName(str.getTagName());
			wxworkTagMapper.insert(dto);
		}

	}
             
	public List<WxworkManualTagVo> getManualTagList(Query query) {
		return	wxworkTagMapper.getManualTagList(query);
		
	}


}
